package com.jushu.storbox.view.viewImpl;

import java.util.Hashtable;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.jushu.storbox.R;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

public class QRImageView extends ImageView {

	private static final int DEFAULT_BLACK = 0xff000000;
	private static final int DEFAULT_WIDTH = 250;
	
	private int qrWidth = DEFAULT_WIDTH;
	private int qrColor = DEFAULT_BLACK;
	private String qrString = "QRView";
	Bitmap bmp = null;
	Bitmap tempBmp = null;
	
	public QRImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		int BARCODE_SIZE = convertDiptoPx(context, DEFAULT_WIDTH);
		final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QRImageView, defStyle, 0);
		qrWidth = a.getInteger(R.styleable.QRImageView_qrWidth, BARCODE_SIZE);
		qrColor = a.getColor(R.styleable.QRImageView_qrColor, DEFAULT_BLACK);
		String tempContent = a.getString(R.styleable.QRImageView_qrContent);
		if(StringUtil.isNotBlank(tempContent)) {
			qrString = tempContent;
		}
		a.recycle();
		render();
	}

	public QRImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public QRImageView(Context context) {
		this(context, null);
	}
	
	/**
	 * generate QR code
	 * @param str   qr code str
	 * @param widthAndHeight 
	 * @return Bitmap bitmap
	 * @throws WriterException
	 */
	public Bitmap createQRCode(int widthAndHeight) throws WriterException {
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
		BitMatrix matrix = new MultiFormatWriter().encode(qrString,
				BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		int[] pixels = new int[width * height];
		
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (matrix.get(x, y)) {
					pixels[y * width + x] = qrColor;
				}
			}
		}
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
		return bitmap;
	}
	
	public int convertDiptoPx(Context context,int dip){
		float scale=context.getResources().getDisplayMetrics().density;
		return (int)(dip*scale+0.5f*(dip>=0?1:-1));
	}

	private void render() {
		try {
			tempBmp = createQRCode(qrWidth);
			if(tempBmp != null) {
				if(bmp != null && !bmp.isRecycled()) {
					bmp.recycle();
				}
				bmp = tempBmp;
				tempBmp = null;
				setImageBitmap(bmp);
			}
		} catch (WriterException e) {
			e.printStackTrace();
		} catch(OutOfMemoryError e) {
			e.printStackTrace();
		}
	}

	public void updateQr(String content) {
		updateQr(content, qrColor, qrWidth);
	}
	
	public void updateQrColor(int color) {
		updateQr(qrString, color, qrWidth);
	}
	
	public void updateQrWidth(int width) {
		updateQr(qrString, qrColor, width);
	}
	
	public void updateQr(String content, int color, int width) {
		boolean isChanged = false;
		
		if(!qrString.equals(content)) {
			if(StringUtil.isBlank(content)) {
				qrString = "";
			} else {
				qrString = content;
			}
			isChanged = true;
		}
		
		if(qrColor != color) {
			qrColor = color;
			isChanged = true;
		}
		
		if(qrWidth != width) {
			if(width > 0) {
				qrWidth = width;
				isChanged = true;
			}
		}

		if(isChanged) {
			render();
		}
	}
}
